package com.maidou.qiantai.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.maidou.qiantai.common.utils.util.WebUtils;
import com.maidou.qiantai.domain.Sku;
import com.maidou.qiantai.domain.Spu;
import com.maidou.qiantai.domain.SpuImage;
import com.maidou.qiantai.dto.member.CartDeleteDto;
import com.maidou.qiantai.dto.member.CartDto;
import com.maidou.qiantai.dto.member.OrderSpuDto;
import com.maidou.qiantai.vo.member.CartVo;
import com.maidou.qiantai.vo.spu.SkuInfoVo;
import com.maidou.qiantai.vo.spu.SpuRecommendVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.maidou.qiantai.mapper.CartMapper;
import com.maidou.qiantai.domain.Cart;
import com.maidou.qiantai.service.ICartService;

import javax.annotation.Resource;

/**
 * 购物车Service业务层处理
 * 
 * @author maidou
 * @date 2024-03-13
 */
@Service
public class CartServiceImpl implements ICartService 
{
    @Resource
    private CartMapper cartMapper;

    @Resource
    private SkuServiceImpl skuService;

    @Resource
    private SpuServiceImpl spuService;

    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public Cart selectCartById(Long id)
    {
        return cartMapper.selectCartById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param cart 购物车
     * @return 购物车
     */
    @Override
    public List<Cart> selectCartList(Cart cart)
    {
        return cartMapper.selectCartList(cart);
    }

    /**
     * 新增购物车
     * 
     * @param cart 购物车
     * @return 结果
     */
    @Override
    public int insertCart(Cart cart)
    {
        cart.setCreateTime(DateUtils.getNowDate());
        return cartMapper.insertCart(cart);
    }

    /**
     * 修改购物车
     * 
     * @param cart 购物车
     * @return 结果
     */
    @Override
    public int updateCart(Cart cart)
    {
        cart.setUpdateTime(DateUtils.getNowDate());
        return cartMapper.updateCart(cart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartByIds(Long[] ids)
    {
        return cartMapper.deleteCartByIds(ids);
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteCartById(Long id)
    {
        return cartMapper.deleteCartById(id);
    }

    /**
     * 查询购物车
     * @return
     */
    @Override
    public List<CartVo> selectCartVoList() {
        Cart cart = new Cart();
        Long memberId = WebUtils.getMemberIdByAccessToken();
        cart.setMemberId(memberId);
        List<Cart> carts = cartMapper.selectCartList(cart);
        List<CartVo> collect = carts.stream().map(item -> {
            CartVo cartVo = new CartVo();
            BeanUtils.copyProperties(item, cartVo);
            Long spuId = item.getSpuId();
            Long skuId = item.getSkuId();
            Sku sku = skuService.selectSkuById(skuId);
            SkuInfoVo skuInfoVo = new SkuInfoVo();
            BeanUtils.copyProperties(sku,skuInfoVo);
            Spu spu = spuService.selectSpuById(spuId);
            SpuRecommendVo spuRecommendVo = new SpuRecommendVo();
            BeanUtils.copyProperties(spu,spuRecommendVo);
            cartVo.setSkuInfoVo(skuInfoVo);
            cartVo.setSpuRecommendVo(spuRecommendVo);
            return cartVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @Override
    public Integer insertDtoCart(CartDto cart) {
        Long memberId = WebUtils.getMemberIdByAccessToken();
        //判断商品是否已经加入过
        Cart cart2 = new Cart();
        cart2.setMemberId(memberId);
        cart2.setSkuId(cart.getSkuId());
        cart2.setSpuId(cart.getSpuId());
        List<Cart> carts = cartMapper.selectCartList(cart2);
        if (carts.size()==0){
            Cart cart1 = new Cart();
            cart1.setMemberId(memberId);
            Date date = new Date();
            Long timestamp = date.getTime();
            cart1.setSort(timestamp);
            cart1.setIsDelete(0);
            BeanUtils.copyProperties(cart,cart1);
            return cartMapper.insertCart(cart1);
        }else {
            for (Cart item:carts
                 ) {
                Date date = new Date();
                Long timestamp = date.getTime();
                item.setSort(timestamp);
                item.setNum(item.getNum()+1);
                cartMapper.updateCart(item);
            }
            return 1;
        }

    }

    /**
     * 修改数量
     * @param id
     * @param num
     * @return
     */
    @Override
    public Integer updateCartNum(Long id, Integer num) {
        Cart cart = new Cart();
        cart.setNum(num);
        cart.setId(id);
        int i = cartMapper.updateCart(cart);
        return i;
    }

    /**
     * 获取订单商品信息
     * @param map
     * @return
     */
    @Override
    public List<CartVo> selectCartVoOrderList(HashMap<String, Object> map) {

        OrderSpuDto orderSpuDto = JSONObject.parseObject(JSON.toJSONString(map), OrderSpuDto.class);
        List<String> cartIds = orderSpuDto.getCartIds();
        List<Long> longs = JSONObject.parseObject(JSONObject.toJSONString(cartIds.get(0)), new TypeReference<List<Long>>() {
        });
        List<Cart> carts = cartMapper.selectCartOrderBatchList(longs);
        List<CartVo> collect = carts.stream().map(item -> {
            CartVo cartVo = new CartVo();
            BeanUtils.copyProperties(item, cartVo);
            Long spuId = item.getSpuId();
            Long skuId = item.getSkuId();
            Sku sku = skuService.selectSkuById(skuId);
            SkuInfoVo skuInfoVo = new SkuInfoVo();
            BeanUtils.copyProperties(sku,skuInfoVo);
            Spu spu = spuService.selectSpuById(spuId);
            SpuRecommendVo spuRecommendVo = new SpuRecommendVo();
            BeanUtils.copyProperties(spu,spuRecommendVo);
            cartVo.setSkuInfoVo(skuInfoVo);
            cartVo.setSpuRecommendVo(spuRecommendVo);
            return cartVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 批量获取购物车信息
     * @param cartIds 购物车id
     * @return
     */
    @Override
    public List<CartVo> selectCartVoBatchListById(List<Long> cartIds) {
        List<Cart> carts = cartMapper.selectCartOrderBatchList(cartIds);
        List<CartVo> collect = carts.stream().map(item -> {
            CartVo cartVo = new CartVo();
            BeanUtils.copyProperties(item, cartVo);
            Long spuId = item.getSpuId();
            Long skuId = item.getSkuId();
            Sku sku = skuService.selectSkuById(skuId);
            SkuInfoVo skuInfoVo = new SkuInfoVo();
            BeanUtils.copyProperties(sku,skuInfoVo);
            Spu spu = spuService.selectSpuById(spuId);
            SpuRecommendVo spuRecommendVo = new SpuRecommendVo();
            BeanUtils.copyProperties(spu,spuRecommendVo);
            cartVo.setSkuInfoVo(skuInfoVo);
            cartVo.setSpuRecommendVo(spuRecommendVo);
            return cartVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
